package org.dentalcard.ui.calendar;

import org.dentalcard.entity.DecUser;
import org.dentalcard.entity.Visit;
import org.dentalcard.entity.WorkDay;
import org.dentalcard.service.PersistenceManager;
import org.dentalcard.service.impl.ScheduleDaoImpl;
import org.dentalcard.ui.api.tools.DecDialog;
import org.dentalcard.ui.i18n.I18nManager;
import org.dentalcard.ui.main.MainFrameBuilder;
import org.dentalcard.ui.tools.DateTextField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Vector;

/**
 * @user: Vladimir Chernishov
 * @date: 25.02.12
 * @time: 20:21
 */
public class AddPatientVisitPopup extends DecDialog {

    private final JPanel main;
    private final DateTextField dateField = new DateTextField();
    private final DateTextField timeField = new DateTextField("HH:mm");
    private final JTextField description = new JTextField();
    private final WorkDay workDay; 
    
    public AddPatientVisitPopup(Frame owner, Date date, WorkDay workDay) {
        super(owner, I18nManager.getRes("add.patient.visit.screen"), false);
        this.main = new JPanel(new GridBagLayout());
        this.getContentPane().add(new JScrollPane(main));
        this.workDay = workDay;
        dateField.setValue(date);
        dateField.setEnabled(false);
        timeField.setValue(new Date());

        setPreferredSize(new Dimension(700, 400));
        setResizable(true);
        init();
        pack();
        center(owner);
    }

    private void init() {
        int rowN = 0;


        main.add(dateField, new GridBagConstraints(0, rowN++, 1 ,1, 0, 0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5), 0, 0));

        main.add(timeField, new GridBagConstraints(0, rowN++, 1 ,1, 0, 0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5), 0, 0));

        main.add(description, new GridBagConstraints(0, rowN++, 1 ,1, 0, 0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5), 0, 0));

        main.add(createOkButton(), new GridBagConstraints(0, rowN++, 1 ,1, 0, 0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5), 0, 0));

    }

    private JButton createOkButton() {
        JButton ok = new JButton("Ok");
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Visit visit = new Visit();
                visit.setTime(timeField.getDate());
                visit.setDescription(description.getText());
                visit.setWorkDayDay(workDay.getDay());
                visit.setWorkDayUserId(workDay.getUserId());
                PersistenceManager.saveOrUpdate(visit);
                ScheduleDaoImpl dao = new ScheduleDaoImpl();
                workDay.setVisits(dao.getVisits(workDay));
                AddPatientVisitPopup.this.dispose();
            }
        });
        return ok;
    }


}
